﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Input;
using System.Windows.Markup;

namespace AESUtil
{
    public class AESUtils
    {
        private static ICryptoTransform GetCipher(int mode)
        {
            byte[] KeyBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.KEY);
            byte[] IvBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.IV);
            using (Aes aes = Aes.Create())
            {
                aes.Key = KeyBytes;
                aes.IV = IvBytes;
                aes.Padding = PaddingMode.PKCS7;
                aes.Mode = CipherMode.CBC;
                if(mode==1)
                    return aes.CreateEncryptor(aes.Key, aes.IV);
                else if(mode==2)
                {
                    return aes.CreateDecryptor(aes.Key,aes.IV);
                }else
                {
                    return null;
                }
            }
        }

        public static String Encrypt(String plainText)
        {
            byte[] KeyBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.KEY);
            byte[] IvBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.IV);
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = KeyBytes;
                aesAlg.IV = IvBytes;

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                        return Convert.ToBase64String(msEncrypt.ToArray());
                    }
                }
            }
        }
        public static String EncryptUseDefaultKeyIV(String plainText)
        {
            byte[] KeyBytes = Encoding.UTF8.GetBytes(Setting.KEY_AES);
            byte[] IvBytes = Encoding.UTF8.GetBytes(Setting.IV_AES);
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = KeyBytes;
                aesAlg.IV = IvBytes;

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                        return Convert.ToBase64String(msEncrypt.ToArray());
                    }
                }
            }
        }
        public static String DecryptUseDefaultKeyIV(String str)
        {
            byte[] KeyBytes = Encoding.UTF8.GetBytes(Setting.KEY_AES);
            byte[] IvBytes = Encoding.UTF8.GetBytes(Setting.IV_AES);
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = KeyBytes;
                aesAlg.IV = IvBytes;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                //byte[] bytes = Convert.FromBase64String(str);
                byte[] bytes = Convert.FromBase64String(str);

                using (MemoryStream msDecrypt = new MemoryStream(bytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            return srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
        }
        public static String Decrypt(String str)
        {
            byte[] KeyBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.KEY);
            byte[] IvBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.IV);
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = KeyBytes;
                aesAlg.IV = IvBytes;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                //byte[] bytes = Convert.FromBase64String(str);
                byte[] bytes = Convert.FromBase64String(str);

                using (MemoryStream msDecrypt = new MemoryStream(bytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            return srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
        }
    }
}
